// zhangpeiyue.com 呈现新闻列表
const http = require("http");
const url = require("url");
const newsList = require("./data");
http.createServer((req,res)=>{
	res.setHeader("Content-Type","text/html;charset=utf-8");
	if(req.url === "/"){
		res.end(newsList.map(info=>(`
			<div>
				<p><a href="/show?id=${info.id}">${info.title}</a></p>
			</div>
		`)).join(""));
	}else if(url.parse(req.url).pathname === "/show"){
		// console.log(url.parse(req.url,true).query);
		const {id} = url.parse(req.url,true).query;
		const info = newsList.find(v=>v.id === id);
		res.end(`
			<div>
				<a href="/">返回首页</a>
				<hr/>
				<h3>${info.title}</h3>
				<p>${info.content}</p>
			</div>
		`);
	}
	else
		res.end("404,您的页面去火星啦！")
}).listen(80,()=>{
	console.log("success");
})